/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.project.logicalViewExtension.computingObjectNodeFactory;


import java.util.ResourceBundle;
import java.util.logging.Logger;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.ui.support.NodeFactory;
import org.netbeans.spi.project.ui.support.NodeFactorySupport;
import org.netbeans.spi.project.ui.support.NodeList;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.Exceptions;
import pl.put.dcs_modeler.exceptions.DCSModelerException;
import pl.put.dcs_modeler.project.logicalViewExtension.computingModelNodeFactory.ComputingModelNodeFactory;
/**
 *
 * @author LukaszR
 */
public class ComputingObjectNodeFactory implements NodeFactory
{
    public static Logger log = Logger.getLogger( ComputingObjectNodeFactory.class.getName() );
    private static final ResourceBundle boundle = ResourceBundle.getBundle( "pl/put/dcs_modeler/exceptions/Boundle" );
    private Project proj;
    
    @Override
    public NodeList<?> createNodes( Project project )
    {
        this.proj = project;
        String projectDir = project.getProjectDirectory().getPath();

        FileObject projectRsrc = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/DCS/DCS Project" );
        String defaultProjectPkg = ( String )projectRsrc.getAttribute( "defaultProjectSrcPackage" );
        ProjectInformation projectInfo = ProjectUtils.getInformation( project );

        // pozyskanie ptrzebnych danych
        String projectName = projectInfo.getName();
        FileObject temp = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/DCS/newComputingObject" );
        String pathToCO = "src/" + defaultProjectPkg + "/" + projectName + "/" + ( String )temp.getAttribute( "defaultPackage" ) ;

        //If there is no 'nbproject' folder,
        //return an empty list of nodes:
        log.info( pathToCO );
        log.info( proj.getProjectDirectory().getPath() );
        
        if ( proj.getProjectDirectory().getFileObject( pathToCO ) == null )
        {
            try
            {

                throw new DCSModelerException( ComputingModelNodeFactory.class.getName(), boundle.getString("Project_is_corrupted._No_models_dir.") );
            }
            catch ( DCSModelerException ex )
            {
                Exceptions.printStackTrace( ex );
            }
            return NodeFactorySupport.fixedNodeList();
        }


        try
        {
            ComputingObjectNode cmn = new ComputingObjectNode( proj, pathToCO );
            cmn.setDisplayName( org.openide.util.NbBundle.getMessage(ComputingObjectNodeFactory.class, "ComputingObjectNodeName") );
            return NodeFactorySupport.fixedNodeList( cmn );
        }
        catch ( DataObjectNotFoundException ex )
        {
            Exceptions.printStackTrace( ex );
            new DCSModelerException( ComputingModelNodeFactory.class.getName(), ex.getMessage() );
        }

        return NodeFactorySupport.fixedNodeList();
    }

}
